home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility1 / gs261src.zip / ECHOGS.C < prev    next >
C/C++ Source or Header  |  1993-05-13  |  6KB  |  209 lines

  1. /* Copyright (C) 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* echogs.c */
  20. /* 'echo'-like utility */
  21. #include <stdio.h>
  22. /* Some brain-damaged environments (e.g. Sun) don't include */
  23. /* prototypes for fputc/fputs in stdio.h! */
  24. extern int fputc(), fputs();
  25. /* Some systems have time_t in sys/types.h rather than time.h. */
  26. #include <sys/types.h>
  27. #include <ctype.h>
  28. #include <string.h>
  29. #include <time.h>        /* for ctime */
  30.  
  31. /*
  32.  * This program exists solely to get around omissions, problems, and
  33.  * incompatibilities in the various shells and utility environments
  34.  * that Ghostscript must deal with.  Don't count on it staying the same
  35.  * from one Ghostscript release to another!
  36.  */
  37.  
  38. /*
  39.  * Usage:
  40.     echogs [-w[b] file | -a[b] file] [-h] [-n]
  41.       (-D | -x hexstring | -q string | -s | -i | -r file)*
  42.       [-] string*
  43.  * Echoes string(s), or the binary equivalent of hexstring(s).
  44.  * If -w, writes to file; if -a, appends to file; if neither,
  45.  * writes to stdout.  -wb and -ab open the file in binary mode.
  46.  * If -h, write the output in hex instead of literally.
  47.  * If -n, does not append a newline to the output.  -s writes a space.
  48.  * -D means insert the date and time.
  49.  * -i means read from stdin, treating each line as an argument.
  50.  * -r means read from a named file in the same way.
  51.  * -X means treat any following literals as hex rather than string data.
  52.  * - alone means treat the rest of the line as literal data,
  53.  * even if the first string begins with a -.
  54.  * Inserts spaces automatically between the trailing strings,
  55.  * but nowhere else; in particular,
  56.     echogs -q a b
  57.  * writes 'ab', in contrast to
  58.     echogs -q a -s b
  59.  * which writes 'a b'.
  60.  */
  61.  
  62. static int hputc(), hputs();
  63.  
  64. main(argc, argv)
  65.     int argc;
  66.     char *argv[];
  67. {    FILE *out = stdout;
  68.     FILE *in;
  69.     char *fmode;
  70.     char *fname = 0;
  71.     int newline = 1;
  72.     int interact = 0;
  73.     int (*eputc)() = fputc, (*eputs)() = fputs;
  74. #define LINESIZE 1000
  75.     char line[LINESIZE];
  76.     char sw = 0, sp = 0, hexx = 0;
  77.     char **argp = argv + 1;
  78.     int nargs = argc - 1;
  79.     if ( nargs > 0 && (*argp)[0] == '-' &&
  80.           ((*argp)[1] == 'w' || (*argp)[1] == 'a')
  81.        )
  82.     {    if ( nargs < 2 ) return 1;
  83.         fmode = *argp + 1;
  84.         fname = argp[1];
  85.         argp += 2, nargs -= 2;
  86.     }
  87.     if ( nargs > 0 && !strcmp(*argp, "-h") )
  88.     {    eputc = hputc, eputs = hputs;
  89.         argp++, nargs--;
  90.     }
  91.     if ( nargs > 0 && !strcmp(*argp, "-n") )
  92.     {    newline = 0;
  93.         argp++, nargs--;
  94.     }
  95.     if ( fname != 0 )
  96.     {    out = fopen(fname, fmode);
  97.         if ( out == 0 ) return 1;
  98.     }
  99.     while ( 1 )
  100.     {    char *arg;
  101.         if ( interact )
  102.         {    if ( fgets(line, LINESIZE, in) == NULL )
  103.             {    interact = 0;
  104.                 if ( in != stdin ) fclose(in);
  105.                 continue;
  106.             }
  107.             /* Remove the terminating \n. */
  108.             line[strlen(line) - 1] = 0;
  109.             arg = line;
  110.         }
  111.         else
  112.         {    if ( nargs == 0 ) break;
  113.             arg = *argp;
  114.             argp++, nargs--;
  115.         }
  116.         if ( sw == 0 && arg[0] == '-' )
  117.         {    sp = 0;
  118.             switch ( arg[1] )
  119.             {
  120.             case 'q':        /* literal string */
  121.             case 'r':        /* read from a file */
  122.             case 'x':        /* hex string */
  123.                 sw = arg[1];
  124.                 break;
  125.             case 's':        /* write a space */
  126.                 (*eputc)(' ', out);
  127.                 break;
  128.             case 'i':        /* read interactively */
  129.                 interact = 1;
  130.                 in = stdin;
  131.                 break;
  132.             case 'D':        /* insert date/time */
  133.             {    time_t t;
  134.                 char str[26];
  135.                 time(&t);
  136.                 strcpy(str, ctime(&t));
  137.                 str[24] = 0;    /* remove \n */
  138.                 (*eputs)(str, out);
  139.             }    break;
  140.             case 'X':        /* treat literals as hex */
  141.                 hexx = 1;
  142.                 break;
  143.             case 0:            /* just '-' */
  144.                 sw = '-';
  145.                 break;
  146.             }
  147.         }
  148.         else
  149.           switch ( sw )
  150.         {
  151.         case 0:
  152.         case '-':
  153.             if ( hexx ) goto xx;
  154.             if ( sp ) (*eputc)(' ', out);
  155.             (*eputs)(arg, out);
  156.             sp = 1;
  157.             break;
  158.         case 'q':
  159.             sw = 0;
  160.             (*eputs)(arg, out);
  161.             break;
  162.         case 'r':
  163.             sw = 0;
  164.             in = fopen(arg, "r");
  165.             if ( in == NULL ) exit(1);
  166.             interact = 1;
  167.             break;
  168.         case 'x':
  169. xx:        {    char *xp;
  170.             unsigned int xchr = 1;
  171.             for ( xp = arg; *xp; xp++ )
  172.             {    char ch = *xp;
  173.                 if ( !isxdigit(ch) ) return 1;
  174.                 xchr <<= 4;
  175.                 xchr += (isdigit(ch) ? ch - '0' :
  176.                      (isupper(ch) ? tolower(ch) : ch)
  177.                       - 'a' + 10);
  178.                 if ( xchr >= 0x100 )
  179.                 {    (*eputc)(xchr & 0xff, out);
  180.                     xchr = 1;
  181.                 }
  182.             }
  183.         }    sw = 0;
  184.             break;
  185.         }
  186.     }
  187.     if ( newline ) (*eputc)('\n', out);
  188.     if ( out != stdout ) fclose(out);
  189.     return 0;
  190. }
  191.  
  192. static int
  193. hputc(ch, out)
  194.     int ch;
  195.     FILE *out;
  196. {    static char *hex = "0123456789abcdef";
  197.     putc(hex[ch >> 4], out);
  198.     putc(hex[ch & 0xf], out);
  199.     return 0;
  200. }
  201.  
  202. static int
  203. hputs(str, out)
  204.     char *str;
  205.     FILE *out;
  206. {    while ( *str ) hputc(*str++ & 0xff, out);
  207.     return 0;
  208. }
  209.